package com.ruoyi.rsikManage.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.rsikManage.domain.AlarmSendingUrlConfig;
import com.ruoyi.rsikManage.service.IAlarmSendingUrlService;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.ServletContext;

/**
 * 报警发往的urlController
 *
 * @author ljw
 * @date 2023-11-06
 */
@RestController
@RequestMapping("/rsikManage/alarmSendingUrl")
public class AlarmSendingUrlController extends BaseController
{
    @Autowired
    private IAlarmSendingUrlService alarmSendingUrlService;
    @Autowired
    ServletContext servletContext;

    /**
     * 查询报警发往的url列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:AlarmSendingUrl:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlarmSendingUrlConfig alarmSendingUrlConfig)
    {
        startPage();
        List<AlarmSendingUrlConfig> list = alarmSendingUrlService.selectAlarmSendingUrlList(alarmSendingUrlConfig);
        return getDataTable(list);
    }

    /**
     * 获取报警发往的url详细信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:AlarmSendingUrl:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(alarmSendingUrlService.selectAlarmSendingUrlById(id));
    }

    /**
     * 新增报警发往的url
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:AlarmSendingUrl:add')")
    //@Log(title = "报警发往的url", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlarmSendingUrlConfig alarmSendingUrlConfig)
    {
        if (alarmSendingUrlConfig.getAlarmType().equals("1")) {
            List<AlarmSendingUrlConfig>  alarmSendingUrlConfigList = (List<AlarmSendingUrlConfig>) servletContext.getAttribute("urlListWhereTheAlarmInfoIsSent");
            if (alarmSendingUrlConfigList == null) {
                alarmSendingUrlConfigList = new ArrayList<>();
            }
            alarmSendingUrlConfigList.add(alarmSendingUrlConfig);
            servletContext.setAttribute("urlListWhereTheAlarmInfoIsSent", alarmSendingUrlConfigList);
        }
        return toAjax(alarmSendingUrlService.insertAlarmSendingUrl(alarmSendingUrlConfig));
    }

    /**
     * 修改报警发往的url
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:AlarmSendingUrl:edit')")
    //@Log(title = "报警发往的url", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlarmSendingUrlConfig alarmSendingUrlConfig)
    {
        List<AlarmSendingUrlConfig>  alarmSendingUrlConfigList = (List<AlarmSendingUrlConfig>) servletContext.getAttribute("urlListWhereTheAlarmInfoIsSent");
        // 查找传入的alarmSendingUrlConfig对象在列表中的索引
        int indexToUpdate = -1;
        for (int i = 0; i < alarmSendingUrlConfigList.size(); i++) {
            if (alarmSendingUrlConfigList.get(i).getId().equals(alarmSendingUrlConfig.getId())) {
                indexToUpdate = i;
                break;
            }
        }
        // 如果找到了匹配的对象
        if (indexToUpdate != -1) {
            // 获取待更新的对象
            AlarmSendingUrlConfig configToUpdate = alarmSendingUrlConfigList.get(indexToUpdate);
            // 如果alarmType为AI报警
            if (alarmSendingUrlConfig.getAlarmType().equals("1")) {
                configToUpdate.setUrl(alarmSendingUrlConfig.getUrl());
            } else {
                alarmSendingUrlConfigList.remove(indexToUpdate);
            }
            servletContext.setAttribute("urlListWhereTheAlarmInfoIsSent", alarmSendingUrlConfigList);
        }

        return toAjax(alarmSendingUrlService.updateAlarmSendingUrl(alarmSendingUrlConfig));
    }

    /**
     * 删除报警发往的url
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:AlarmSendingUrl:remove')")
    //@Log(title = "报警发往的url", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        List<AlarmSendingUrlConfig>  alarmSendingUrlConfigList = (List<AlarmSendingUrlConfig>) servletContext.getAttribute("urlListWhereTheAlarmInfoIsSent");
        List<AlarmSendingUrlConfig> filteredList = alarmSendingUrlConfigList.stream()
                .filter(urlConfig -> !Arrays.asList(ids).contains(urlConfig.getId()))
                .collect(Collectors.toList());
        servletContext.setAttribute("urlListWhereTheAlarmInfoIsSent", filteredList);

        return toAjax(alarmSendingUrlService.deleteAlarmSendingUrlByIds(ids));
    }

}
